package cn.laixueit.listener;

import cn.laixueit.service.SearchService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class GoodsListener {

    @Autowired
    private SearchService searchService;

    /**
     * 更新新增
     * @param id
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "chuangmei.index.create.queue",durable = "true"),
            exchange = @Exchange(value = "chuangmei.item.exchange",ignoreDeclarationExceptions = "true",type = ExchangeTypes.TOPIC),
            key = {"item.insert","item.update"}
    ))
    public void listenerInsert(Long id){
        if(id == null){
            return;
        }
        // 创建或者更新索引
        searchService.createOrUpdateIndex(id);
    }


    /**
     * 删除
     * @param id
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "chuangmei.index.create.queue",durable = "true"),
            exchange = @Exchange(value = "chuangmei.item.exchange",ignoreDeclarationExceptions = "true",type = ExchangeTypes.TOPIC),
            key = {"item.delete"}
    ))
    public void listenerDelete(Long id){
        searchService.deleteIndex(id);
    }
}
